package com.itheima.springboottilasmanager.controller;

import com.itheima.springboottilasmanager.pojo.domain.Dept;
import com.itheima.springboottilasmanager.pojo.domain.Result;
import com.itheima.springboottilasmanager.survice.deptsurvice.Depts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/depts")
@RestController
@Slf4j
public class DeptController {
    @Autowired
    Depts dept;
    @GetMapping
    public Result findall() {
        List<Dept> list = dept.findall();
         log.info("查询所有部门");
        return Result.success(list);
    }

//    @RequestMapping(value = "/depts", method = RequestMethod.DELETE)
    @DeleteMapping
    public Result deleteDept(@RequestParam Integer id) {

        dept.remove(id);
        return Result.success() ;
    }

//    @RequestMapping(value = "/depts", method = RequestMethod.POST)
    @PostMapping
    public Result addDept(@RequestBody Dept d1) {
        dept.saveByID(d1);

        return Result.success();
    }

//    @RequestMapping(value = "/depts", method = RequestMethod.PUT)
    @PutMapping
    public Result updateDept(@RequestBody Dept d1) {
        boolean success = dept.updateDepts(d1);
        if (success) {
            return Result.success(d1); // 返回更新后的对象
        }
        return Result.error("shibai");
    }




//    添加单个数组
//    @RequestMapping(value = "/depts", method = RequestMethod.PUT)
//    public Result updateDept(@RequestBody List<Dept> depts) {
//        if (depts == null || depts.isEmpty()) {
//            return Result.error("没有数据");
//        }
//        Dept d1 = depts.get(0); // 只处理数组的第一个元素
//        boolean success = dept.updateDepts(d1);
//        if (success) {
//            return Result.success(d1);
//        }
//        return Result.error("更新失败");
//    }




//    @RequestMapping(value = "/depts/{id}", method = RequestMethod.GET)
    @GetMapping("/{id}")
    public Result selectCount( @PathVariable Integer id) {
//        List<Dept> list = dept.selectCount(id);
//        return Result.success(list);
        Dept deptObj = dept.selectCount(id);  // 直接查单个对象
        return Result.success(deptObj);
    }
//    定义处理器方法，处理更新部门请求
}


